package com.gegong.vmware.workstation.service.impl;

import com.gegong.vmware.workstation.CopyVmRequestParameter;
import com.gegong.vmware.workstation.VMInformation;
import com.gegong.vmware.workstation.VirtualMachine;
import com.gegong.vmware.workstation.exceptions.VMwareWorkstationManagementException;
import com.gegong.vmware.workstation.service.VMManagementService;
import com.gegong.vmware.workstation.service.VMNetworkAdaptersManagementService;
import com.gegong.vmware.workstation.service.VMPowerManagementService;
import com.gegong.vmware.workstation.utils.VmxFileUtils;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.util.List;

import static com.gegong.vmware.workstation.VMwareWorkstationManagementActions.VM;

public class DefaultVMManagementServiceImpl implements VMManagementService {


    @Override
    public VirtualMachine findById(String vmId) throws VMwareWorkstationManagementException {
        for (VirtualMachine vm : findAll()) {
            if (vm.getId().equals(vmId)) {
                return vm;
            }
        }
        throw new VMwareWorkstationManagementException(String.format("没有找到id为%s的虚拟机", vmId));
    }

    @Override
    public VirtualMachine findByName(String vmName) throws VMwareWorkstationManagementException {
        for (VirtualMachine vm : findAll()) {
            VmxFileUtils vmxFileUtils = new VmxFileUtils(vm.getPath());
            if (vmxFileUtils.getDsplayName().equals(vmName)) {
                return vm;
            }
        }

        throw new VMwareWorkstationManagementException(String.format("没有找到名称为%s的虚拟机", vmName));
    }

    @Override
    public String getVmNameById(String vmId) throws VMwareWorkstationManagementException {
        for (VirtualMachine vm : findAll()) {
            if (vm.getId().equals(vmId)) {
                VmxFileUtils vmxFileUtils = new VmxFileUtils(vm.getPath());
                return vmxFileUtils.getDsplayName();
            }
        }
        return null;
    }


    public List<VirtualMachine> findAll() throws VMwareWorkstationManagementException {

        String url = getUrl(VM.FIND_ALL_VMS);
        Request request = new Request.Builder()
                .url(url)
                .headers(defaultHeaders)
                .build();
        return getResponse(request, VM.FIND_ALL_VMS).getListData(VirtualMachine.class);
    }

    @Override
    public VMInformation copyVm(CopyVmRequestParameter requestParameter) throws VMwareWorkstationManagementException {
        String url = getUrl(VM.COPY_VM);
        RequestBody requestBody = RequestBody.create(defaultPostMediaType, requestParameter.toJsonString().getBytes(Charsets.UTF_8));
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .headers(defaultPostHeaders)
                .build();
        return getResponse(request, VM.COPY_VM).getData(VMInformation.class);
    }


    @Override
    public void deleteVm(String id) throws VMwareWorkstationManagementException {
        String url = getUrl(VM.DELETE_VM, id);
        Request request = new Request.Builder()
                .url(url)
                .headers(defaultHeaders)
                .delete()
                .build();
        getResponse(request, VM.DELETE_VM);
    }


}
